package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.orhon.smartcampus.modules.systemctl.dao.ApisDao;
import com.orhon.smartcampus.modules.systemctl.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.dao.WidgetsDao;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLWidgetsInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.WidgetsInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddWidgetsMutation implements DataFetcher {

    @Autowired
    WidgetsDao widgetsDao;

    @Autowired
    MenusDao menusDao;

    @Autowired
    ModulesDao modulesDao;

    @Autowired
    ApisDao apisDao;

    /**
     * 新增/修改页面组件
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLWidgetsInput input = WidgetsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Widgets widgets = null;

        if (input.getId() != null) {
            widgets = widgetsDao.get(Widgets.class, input.getId());
            widgets.setUpdatedAt(new Date());
        } else {
            widgets = new Widgets();
            widgets.setCreatedAt(new Date());
        }
        if (input.getApis().size()>0){
            List<Apis> apis = new ArrayList<>();
            input.getApis().forEach(api_id->{
                try {
                    Apis newapis = apisDao.findById(api_id);
                    apis.add(newapis);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            widgets.setApis(apis);
        }
        if(input.getTitle() != null){

            widgets.setTitle(JSON.toJSONString(input.getTitle()));
        }
        if(input.getName() != null){

            widgets.setName(input.getName());
        }
        if(input.getType() != null){

            widgets.setType(input.getType());
        }
        if(input.getMenu_id() != null) {
            widgets.setSystemctlMenusByMenuId(menusDao.get(Menus.class, input.getMenu_id()));
        }
        if(input.getModule_id() != null) {
            widgets.setSystemctlModulesByModuleId((Modules) modulesDao.get(Modules.class, input.getModule_id()));
        }
        if(input.getClients() != null){

            widgets.setClients(JSON.toJSONString(input.getClients()));
        }
        if(input.getMeta() != null){

            widgets.setMeta(input.getMeta());
        }
        if(input.getStatus() != null){

            widgets.setStatus(input.getStatus());
        }
        try{
            widgetsDao.saveOrUpdate(widgets);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

